# Replication archive for: Coppock, Alexander and Seth J. Hill and Lynn Vavreck. 2020. "The Small Effects of Political Advertising are Small Regardless of Context, Message, Sender, or Receiver: Evidence from 59 Real-time Randomized Experiments" Science Advances, forthcoming.

library(tidyverse)
library(estimatr)
source("helpers.R")

exps <- read_rds("exps.rds")
ad_df <- read_csv("ad_df.csv")

# Run all regressions -----------------------------------------------------

CATEs <-
  bind_rows(
    exps %>% group_by(pid_3_pre, battleground) %>% do(estimate_ad_effects(., "favorDT_rev")),
    exps %>% group_by(pid_3_pre, battleground) %>% do(estimate_ad_effects(., "favorHC_rev")),
    exps %>% group_by(pid_3_pre, battleground) %>% do(estimate_ad_effects(., "favorBS_rev")),
    exps %>% group_by(pid_3_pre, battleground) %>% do(estimate_ad_effects(., "favorJK_rev")),
    exps %>% group_by(pid_3_pre, battleground) %>% do(estimate_ad_effects(., "favorTC_rev"))
  ) %>%
  filter(grepl("ad_id_fac", term)) %>%
  mutate(ad_id = as.numeric(gsub("ad_id_fac", "", term))) %>%
  left_join(ad_df) %>%
  ungroup %>%
  mutate(
    target_candidate = (outcome == "favorDT_rev" & ad_valence %in% c("Pro Trump", "Anti Trump") |
                          outcome == "favorHC_rev" & ad_valence %in% c("Pro Clinton", "Anti Clinton") |
                          outcome == "favorBS_rev" & ad_valence %in% c("Pro Sanders", "Anti Sanders") |
                          outcome == "favorJK_rev" & ad_valence %in% c("Pro Kasich", "Anti Kasich") |
                          outcome == "favorTC_rev" & ad_valence %in% c("Pro Cruz", "Anti Cruz")),
    attacking_candidate = 
      (outcome == "favorDT_rev" & ad_valence %in% c("Anti Clinton") |
         outcome == "favorHC_rev" & ad_valence %in% c("Anti Trump")),
    estimate_type = case_when(
      target_candidate ~ "target_candidate",
      attacking_candidate ~ "attacking_candidate",
      TRUE ~ NA_character_
    ),
    attack = as.numeric(grepl("Anti", ad_valence)),
    general = as.numeric(date > as.Date("2016-06-10")),
    pro_clinton = as.numeric(ad_valence == "Pro Clinton"),
    pro_sanders = as.numeric(ad_valence == "Pro Sanders"),
    pro_cruz = as.numeric(ad_valence == "Pro Cruz"),
    pro_kasich = as.numeric(ad_valence == "Pro Kasich"),
    pro_trump = as.numeric(ad_valence == "Pro Trump"),
    
    anti_clinton = as.numeric(ad_valence == "Anti Clinton"),
    anti_trump = as.numeric(ad_valence == "Anti Trump"),
    date_numeric = as.numeric(date) / 31,
    attack_d = attack - mean(attack),
    general_d = general - mean(general),
    
    pro_clinton_d = pro_clinton - mean(pro_clinton),
    pro_sanders_d = pro_sanders - mean(pro_sanders),
    pro_trump_d = pro_trump - mean(pro_trump),
    pro_cruz_d = pro_cruz - mean(pro_cruz),
    pro_kasich_d = pro_kasich - mean(pro_kasich),
    anti_clinton_d = anti_clinton - mean(anti_clinton),
    anti_trump_d = anti_trump - mean(anti_trump),
    
    pac = if_else(ad_sponsor %in% c("clinton", "cruz", "sanders", "trump"), 0, 1),
    pac_d = pac - mean(pac),
    
    date_d = date_numeric - mean(date_numeric),
    attack_general_d = attack_d*general_d - mean(attack_d*general_d),
    attack_date_d = attack_d*date_d - mean(attack_d*date_d),
    
    pro_democrat = as.numeric(ad_valence %in% c("Anti Trump", "Pro Clinton", "Pro Sanders")),
    pro_republican = as.numeric(ad_valence %in% c("Pro Cruz", "Pro Kasich", "Pro Trump", "Anti Clinton")),
    
    democrat = as.numeric(pid_3_pre == "Democrat"),
    republican = as.numeric(pid_3_pre == "Republican"),
    independent = as.numeric(pid_3_pre == "Independent"),
    
    attack_d = attack - mean(attack),
    date_d = date_numeric - mean(date_numeric),
    attack_date_d = attack_d*date_d - mean(attack_d*date_d),
    
    pro_democrat_d = pro_democrat - mean(pro_democrat),
    pro_republican_d = pro_republican - mean(pro_republican),
    democrat_d = democrat - mean(democrat),
    republican_d = republican - mean(republican),
    independent_d = independent - mean(independent),
    
    battleground_d = battleground - mean(battleground),
    
    ad_match = 
      case_when(
        democrat == 1 & pro_democrat == 1 ~ "Democratic respondents, pro-Democratic ad",
        democrat == 1 & pro_republican == 1 ~ "Democratic respondents, pro-Republican ad",
        independent == 1 & pro_democrat == 1 ~ "Independent respondents, pro-Democratic ad",
        independent == 1 & pro_republican == 1 ~ "Independent respondents, pro-Republican ad",
        republican == 1 & pro_democrat == 1 ~ "Republican respondents, pro-Democratic ad",
        republican == 1 & pro_republican == 1 ~ "Republican respondents, pro-Republican ad"
      )
    
  )

# The recode happens NOW
  
favorability_df <- 
  CATEs %>% 
  filter(target_candidate) %>%
  mutate(
    estimate = if_else(attack == 1, estimate * -1, estimate),
    conf.low = if_else(attack == 1, conf.low * -1, conf.low),
    conf.high = if_else(attack == 1, conf.high * -1, conf.high),
    date_group = as.numeric(as.factor(date))
  )

write_rds(favorability_df, path = "favorability_cates.rds")

